﻿namespace Cmn.Cls
{
    using System;
    using System.Data;
    using System.IO;
    using System.Xml;

    public class Xml
    {
        private int _State = 0;
        private XmlDocument objXmlDoc = new XmlDocument();
        private string strXmlFile;

        public Xml(string XmlFile)
        {
            this.strXmlFile = XmlFile;
            try
            {
                this.objXmlDoc.Load(XmlFile);
            }
            catch
            {
                this._State = 1;
            }
        }

        public void addAttr(string Node, string attributeName, string attributeValue)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this._State = 3;
            }
            else
            {
                XmlElement element = (XmlElement) this.objXmlDoc.SelectSingleNode(xpath);
                try
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (element.Attributes[strArray[i]] == null)
                        {
                            element.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                catch
                {
                    this._State = 4;
                }
            }
        }

        public void addNode(string MainNode, string Node, string attributeName, string attributeValue)
        {
            string xpath = MainNode.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this._State = 3;
            }
            else
            {
                XmlNode node = this.objXmlDoc.SelectSingleNode(xpath);
                XmlElement newChild = this.objXmlDoc.CreateElement(Node);
                if (attributeName.Trim() != "")
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (node.Attributes[strArray[i]] == null)
                        {
                            newChild.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                node.AppendChild(newChild);
            }
        }

        public void addNode(string MainNode, string Node, string attributeName, string attributeValue, string Content)
        {
            string xpath = MainNode.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this._State = 3;
            }
            else
            {
                XmlNode node = this.objXmlDoc.SelectSingleNode(xpath);
                XmlElement newChild = this.objXmlDoc.CreateElement(Node);
                if (attributeName.Trim() != "")
                {
                    for (int i = 0; i <= (strArray.Length - 1); i++)
                    {
                        if (node.Attributes[strArray[i]] == null)
                        {
                            newChild.SetAttribute(strArray[i], strArray2[i]);
                        }
                    }
                }
                newChild.InnerText = Content;
                node.AppendChild(newChild);
            }
        }

        public string getAttr(string Node, string attributeName)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            string str2 = "";
            string[] strArray = attributeName.Split(new char[] { '|' });
            XmlNode node = this.objXmlDoc.SelectSingleNode(xpath);
            for (int i = 0; i <= (strArray.Length - 1); i++)
            {
                try
                {
                    str2 = str2 + node.Attributes[strArray[i]].Value.ToString() + "|";
                }
                catch
                {
                    str2 = str2 + "|";
                }
            }
            return str2.Substring(0, str2.Length - 1);
        }

        public DataSet getData()
        {
            DataSet set = new DataSet();
            set.ReadXml(this.strXmlFile);
            return set;
        }

        public DataSet getData(string Node)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            DataSet set = new DataSet();
            StringReader reader = new StringReader(this.objXmlDoc.SelectSingleNode(xpath).OuterXml);
            set.ReadXml(reader);
            return set;
        }

        public string getNodeText(string Node)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            return this.objXmlDoc.SelectSingleNode(xpath).InnerText;
        }

        public void Save()
        {
            try
            {
                if (this._State == 0)
                {
                    this.objXmlDoc.Save(this.strXmlFile);
                }
            }
            catch
            {
                this._State = 2;
            }
            this.objXmlDoc = null;
        }

        public void setAttr(string Node, string attributeName, string attributeValue)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            string[] strArray = attributeName.Split(new char[] { '|' });
            string[] strArray2 = attributeValue.Split(new char[] { '|' });
            if (strArray2.Length != strArray.Length)
            {
                this._State = 3;
            }
            else
            {
                XmlNode node = this.objXmlDoc.SelectSingleNode(xpath);
                for (int i = 0; i <= (strArray.Length - 1); i++)
                {
                    try
                    {
                        node.Attributes[strArray[i]].Value = strArray2[i];
                    }
                    catch
                    {
                    }
                }
            }
        }

        public void setNodeText(string Node, string Content)
        {
            string xpath = Node.TrimEnd(new char[] { '/' });
            this.objXmlDoc.SelectSingleNode(xpath).InnerText = Content;
        }

        public int State
        {
            get
            {
                return this._State;
            }
        }
    }
}

